import Vue from 'vue'
import axios from 'axios'

const Api = ''// 'http://108.160.143.21:8080/bookbar'//接口地址
const Size = 2

axios.defaults.timeout = 30000// axios 响应时间
// axios.defaults.withCredentials = true

axios.interceptors.request.use(
  config => {
    // if (store.state.token) {
    //   config.headers.Authorization = store.state.token
    // }
    // config.headers.Authorization = 'dev'
    return config
  },
  err => {
    return Promise.reject(err)
  }
)

// RESPONSE 响应异常拦截
axios.interceptors.response.use(data => {
  if (data.status && data.status === 200 && data.data.status === 'error') {
    Vue.$vux.toast.text(data.data.msg, 'bottom')
    return
  }
  return data
}, err => {
  if (err && err.response) {
    switch (err.response.status) {
      case 400:
        err.message = '请求错误(400)'
        break
      case 401:
        err.message = '未授权，请重新登录(401)'
        break
      case 403:
        err.message = '拒绝访问(403)'
        break
      case 404:
        err.message = '请求出错(404)'
        break
      case 408:
        err.message = '请求超时(408)'
        break
      case 500:
        err.message = '服务器错误(500)'
        break
      case 501:
        err.message = '服务未实现(501)'
        break
      case 502:
        err.message = '网络错误(502)'
        break
      case 503:
        err.message = '服务不可用(503)'
        break
      case 504:
        err.message = '网络超时(504)'
        break
      case 505:
        err.message = 'HTTP版本不受支持(505)'
        break
      default:
        err.message = `连接出错(${err.response.status})!`
    }
  } else {
    err.message = '连接服务器失败!'
  }
  Vue.$vux.toast.text(err.message, 'bottom')
  return Promise.resolve(err)
})

// 个人信息
export function UserInfo () {
  return axios.get(Api + '/api/v1/authc/app/member/self', {
    headers: {
      'Content-Type': 'application/json;charset=UTF-8'
    }
  })
}

// 登录
export function Login () {
  return axios.get(Api + '/api/v1/anon/app/member/1/debug')
}

// 修改个人信息(不能修改电话)
export function EditUserInfo (data) {
  return axios.patch(Api + '/api/v1/authc/app/member/self', {
    ...data
  })
}

// 完善个人信息
export function EditInfo (data) {
  return axios.post(Api + '/api/v1/authc/app/member/self', {
    ...data
  })
}

// 修改手机号码
export function EditTel (data) {
  return axios.patch(Api + '/api//v1/authc/app/member/tel', {
    ...data
  }, {
    headers: {
      'Content-Type': 'application/json;charset=UTF-8'
    }
  })
}

// 获取手机验证码
export function GetCode (data) {
  return axios.get(Api + '/api/v1/authc/app/mobile-verify-code', {
    params: {
      ...data
    }
  })
}

// 我的卡券
export function MyCoupon () {
  return axios.get(Api + '/api/v1/authc/app/member/coupon')
}

// 充值介绍
export function RechargeInfo () {
  return axios.get(Api + '/api/v1/authc/app/recharge-summary')
}

// 套餐列表
export function MealList () {
  return axios.get(Api + '/api/v1/authc/app/set-meal')
}

// 书吧列表
export function BookList (data) {
  return axios.get(Api + '/api/v1/authc/app/shop', {
    params: {
      ...data
    }
  })
}

// 查询会员充值记录
export function MoneyRecord (data) {
  return axios.get(Api + '/api/v1/authc/app/member/recharge', {
    params: {
      ...data
    }
  })
}

// 订座记录
export function SeatRecord (data) {
  return axios.get(Api + '/api/v1/authc/app/member/order', {
    params: {
      ...data
    }
  })
}

// 获取公告栏信息
export function Notice () {
  return axios.get(Api + '/api/v1/authc/app/notice')
}

// 获取系统消息
export function SysMessage (data) {
  return axios.get(Api + '/api/v1/authc/app/sys-message', {
    params: {
      ...data
    }
  })
}

// 充值金额使用优惠券
export function BestMatch (data) {
  return axios.get(Api + '/api/v1/authc/app/recharge-rule/best-match', {
    params: {
      ...data
    }
  })
}

// 套餐使用优惠券
export function SetMeal (data) {
  return axios.get(Api + '/api/v1/authc/app/member/coupon/set-meal', {
    params: {
      ...data
    }
  })
}

// 获取套餐使用卡券的优惠
export function Discounted (data) {
  return axios.get(Api + `/api/v1/authc/app/set-meal/${data.setMealId}/discounted`, {
    params: {
      'seat-type': data.seatType,
      'coupon-id': data.couponId
    }
  })
}

// 获取可领取的卡券
export function Canget () {
  return axios.get(Api + `/api/v1/authc/app/member/coupon/can-get`)
}

// 领取卡券
export function Getcoupon (data) {
  return axios.post(Api + `/api/v1/authc/app/member/coupon/${data.couponId}`)
}

// 金额充值
export function Balance (data) {
  return axios.post(Api + `/api/v1/authc/app/member/recharge/balance`, {
    ...data
  })
}

// 套餐充值
export function SendSetMeal (data) {
  return axios.post(Api + `/api/v1/authc/app/member/recharge/setmeal`, {
    ...data
  })
}

// 预约订座
export function YakusokuOrder (data) {
  return axios.post(Api + `/api/v1/authc/app/member/order`, {
    ...data
  })
}

// 订单查询
export function DnotuseOrder (data) {
  return axios.get(Api + `/api/v1/authc/app/member/order`, {
    params: {
      ...data
    }
  })
}

// 微信授权sdk
export function wxAdmin (data) {
  return axios.get(Api + `/api/v1/authc/app/jsapi-sign`, {
    params: {
      ...data
    }
  })
}

// 扫码入场
export function ScanEntry (data) {
  console.log(data)
  return axios.get(Api + `/api/v1/authc/app/member/order/${data.orderId}/scan-entry?qrcode=${data.qrcode}`)
}

// 开始计时
export function OrderStart (data) {
  return axios.patch(Api + `/api/v1/authc/app/member/order/${data.orderId}/start?qrcode=${data.qrcode}`)
}

// 暂停计时
export function PauseTime (data) {
  return axios.patch(Api + `/api/v1/authc/app/member/order/${data.orderId}/seat/${data.orderSeatId}/pause`, {
    orderId: data.orderId,
    orderSeatId: data.orderSeatId
  })
}

// 恢复计时
export function ContinueTime (data) {
  return axios.patch(Api + `/api/v1/authc/app/member/order/${data.orderId}/seat/${data.orderSeatId}/resume`, {
    orderId: data.orderId,
    orderSeatId: data.orderSeatId
  })
}

// 结账
export function Bill (data) {
  return axios.patch(Api + `/api/v1/authc/app/member/order/${data.orderId}/bill`, {
    orderId: data.orderId
  })
}

// 座位列表
export function GetSeat (data) {
  return axios.get(Api + `/api/v1/authc/app/shop/${data.shopId}/seat`, {
    params: {
      shopId: data.shopId,
      day: data.day
    }
  })
}

// 座位预检查
export function PreCheck (data) {
  return axios.post(Api + `/api/v1/authc/app/member/order/precheck`, {
    ...data
  })
}

// 确认订单
export function Confirms (data) {
  return axios.patch(Api + `/api/v1/authc/app/member/order/${data.orderId}/confirm`, {
    orderId: data.orderId
  })
}

// 预约订座
export function ConfirmSeat (data) {
  return axios.post(Api + `/api/v1/authc/app/member/order`, {
    ...data
  })
}

// 根据订单id查询
export function QueryOrder (data) {
  return axios.get(Api + `/api/v1/authc/app/member/order/${data.orderId}`, {
    ...data
  })
}

// 获取可预约的时间
export function TimeRange (data) {
  return axios.post(Api + `/api/v1/authc/app/order/time-range`, {
    ...data
  })
}

// 支付宝充值查询
export function Alipay (data) {
  return axios.get(Api + `/api/v1/authc/app/member/recharge/${data.id}`, {
    ...data
  })
}

// 修改头像
export function UploadImg (data) {
  console.log(data)
  return axios.post(Api + `/api/v1/authc/member/avatar`, data, {
    headers: {'Content-Type': 'multipart/form-data'}
  })
}

export function ApiUrl () {
  return Api
}

export function SizeImg () {
  return Size
}
